package com.blb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Product;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IProductService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("product_check")
public class ProductCheckController {

    @Autowired
    private IProductService productService;

    private String pNumber0;

    /**
     * 跳转到商品审核页面
     * @return
     */
    @GetMapping("index")
    public String index(){
        return "products_check/products_check";
    }

    /**
     * 分页列表
     * @param productName
     * @param start
     * @param length
     * @return
     */
    @ResponseBody
    @PostMapping("page")
    public BlbPage page(String productName, int start, int length){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        //查询分页数据
        IPage page = productService.page(new Page(start, length), new QueryWrapper<Product>().like("p_name", productName).eq("company_id",companyId));

        //查询总条数
        int count = productService.count(new QueryWrapper<Product>().like("p_name", productName).eq("company_id",companyId));

        return new DataTablePage(count, count, page.getRecords());
    }
    /**
     * 查看商品详情  传的商品Id
     * @return
     */
    @PostMapping("pNumber")
    @ResponseBody
    private ResponseHelp pNumber(@Param("pNumber") String pNumber) {
        Product product = productService.getProductByPNumber(pNumber);
        System.out.println("商品详情"+product);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(product);
        return responseHelp;
    }
    /**
     * 通过审核
     * @return
     */
    @PostMapping("yaudit")
    @ResponseBody
    private ResponseHelp yaudit(@Param("pNumber") String pNumber) {
        System.out.println("pNumber");
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        String aStatus = "1";
        productService.audit(pNumber,aStatus,companyId);
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;
    }
    /**
     * 不通过审核
     * @return
     */
    @PostMapping("naudit")
    @ResponseBody
    private ResponseHelp naudit(@Param("pNumber") String pNumber) {
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        String aStatus = "0";

        String pStatus = productService.getpStatus(pNumber, companyId);
        /*商品状态为下架的*/
        if (pStatus.equals("-1")){
            productService.audit(pNumber,aStatus,companyId);
        }else if(pStatus.equals("1")) {
            /*商品状态为上架的*/
            String pStatus2 = "-1";
            productService.audit2(pNumber,aStatus,pStatus2,companyId);
        }
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;
    }
    /**
     * 待审核数量
     * @return
     */
    @PostMapping("noAudit")
    @ResponseBody
    private ResponseHelp noAudit() {
        System.out.println("待审核数量");
        User user = ShiroUtils.getUser();
        System.out.println(user);
        String companyId = user.getCompanyId();
        System.out.println("公司id"+companyId);
        int noAuditNum = productService.noAudit(companyId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(noAuditNum);
        System.out.println(responseHelp);
        return responseHelp;
    }
    /**
     * 已审核数量
     * @return
     */
    @PostMapping("yetAudit")
    @ResponseBody
    private ResponseHelp yetAudit() {
        System.out.println("已审核数量");
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        int notAuditNum = productService.notAudit(companyId);
        int yetAuditNum1 = productService.yetAudit(companyId);
        int yetAuditNum = yetAuditNum1 + notAuditNum;
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(yetAuditNum);
        return responseHelp;
    }

    /**
     * 未通过数量
     * @param model
     * @return
     */
    @PostMapping("notAudit")
    @ResponseBody
    private ResponseHelp notAudit(Model model) {
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        int notAuditNum = productService.notAudit(companyId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(notAuditNum);
        return responseHelp;

    }


}
